<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class File {
	private $CI;
	
	public function __construct() {
		$this->CI = &get_instance();
		$this->CI->load->model('File_Model', 'filemodel');
	}
	
	public function check_file($id, $username) {
		return $this->CI->filemodel->check_file($id, $username);
	}
	
	public function get_files($username) {
		return $this->CI->filemodel->get_files($username);
	}
	
	public function get_category_by_username_and_id($username, $id) {
		return $this->CI->filemodel->get_category_by_username_and_id($username, $id);
	}
	
	public function get_category_by_id($id) {
		return $this->CI->filemodel->get_category_by_id($id);
	}
	
	public function get_categories() {
		return $this->CI->filemodel->get_categories();
	}
	
	public function update_student_file($id, $username, $details) {
		if ($this->CI->authentication->is_allowed('EDIT_DOCUMENT')) {
			$status = $this->CI->filemodel->get_category_by_username_and_id($username, $id);
			if ($status == NULL) {
				return $this->CI->filemodel->insert_student_file($id, $username, $details);
			} else {
				return $this->CI->filemodel->update_student_file($id, $username, $details);	
			}
		} else {
			$this->CI->session->set_flashdata('error', 'Role tidak boleh mengakses fitur ini.');
			return FALSE;
		}
	}
	
	public function delete_student_file($id, $username, $name) {
		if ($this->CI->authentication->is_allowed('EDIT_DOCUMENT')) {
			return $this->CI->filemodel->delete_student_file($id, $username, $name);
		} else {
			$this->CI->session->set_flashdata('error', 'Role tidak boleh mengakses fitur ini.');
			return FALSE;
		}
	}
	
	public function is_unique_files($files) {
		return $this->CI->filemodel->is_unique_files($files);
	}
}